<?php
if (!defined('IN_IA')) {
	exit('Access Denied');
}
class Shop_UnporBabyModel{
	/**
	 * 获取商品分类
	 * @global type $_W
	 * @return type
	 */
	public function getCategory($refresh = false)
	{
		global $_W;
		//$allcategory = m('cache')->getArray('allcategory');
		if (empty($allcategory) || $refresh) {
			$parents = array();
			$children = array();
			$category = pdo_fetchall('SELECT * FROM ' . tablename('imfox_eos_category') . ' WHERE uniacid =:uniacid AND enabled=1 ORDER BY parentid ASC, displayorder DESC', array(':uniacid' => $_W['uniacid']));

			foreach ($category as $index => $row) {
				if (!empty($row['parentid'])) {
					if ($row[$row['parentid']]['parentid'] == 0) {
						$row[$row['parentid']]['level'] = 2;
					}
					else {
						$row[$row['parentid']]['level'] = 3;
					}

					$children[$row['parentid']][] = $row;
					unset($category[$index]);
				}
				else {
					$row['level'] = 1;
					$parents[] = $row;
				}
			}

			$allcategory = array('parent' => $parents, 'children' => $children);
			//m('cache')->set('allcategory', $allcategory);
		}

		return $allcategory;
	}

	public function getFullCategory($fullname = false, $enabled = false)
	{
		global $_W;
		//$allcategorynames = m('cache')->getArray('allcategorynames');
		//$shopset = m('common')->getSysset('shop');
		$allcategory = array();
		$sql = 'SELECT * FROM ' . tablename('imfox_eos_category') . ' WHERE uniacid=:uniacid ';

		if ($enabled) {
			$sql .= ' AND enabled=1';
		}

		$sql .= ' ORDER BY parentid ASC, displayorder DESC';
		$category = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));
		//$category = set_medias($category, array('thumb', 'advimg'));

		if (empty($category)) {
			return array();
		}

		foreach ($category as &$c) {
			if (empty($c['parentid'])) {
				$allcategory[] = $c;

				foreach ($category as &$c1) {
					if ($c1['parentid'] != $c['id']) {
						continue;
					}

					if ($fullname) {
						$c1['name'] = $c['name'] . '-' . $c1['name'];
					}

					$allcategory[] = $c1;

					foreach ($category as &$c2) {
						if ($c2['parentid'] != $c1['id']) {
							continue;
						}

						if ($fullname) {
							$c2['name'] = $c1['name'] . '-' . $c2['name'];
						}

						$allcategory[] = $c2;

						foreach ($category as &$c3) {
							if ($c3['parentid'] != $c2['id']) {
								continue;
							}

							if ($fullname) {
								$c3['name'] = $c2['name'] . '-' . $c3['name'];
							}

							$allcategory[] = $c3;
						}

						unset($c3);
					}

					unset($c2);
				}

				unset($c1);
			}

			unset($c);
		}

		return $allcategory;
	}
	public function getCategoryName($category_id,$fullname = false){
		global $_W;
		$category = pdo_get("imfox_eos_category",array('uniacid'=>$_W['uniacid'],'id'=>$category_id));
		if(empty($category['name'])){
			return "未知分类";
		}
		$categoryName = $category['name'];
		if($fullname && $category['parentid']>0){
			$parent = pdo_get("imfox_eos_category",array('uniacid'=>$_W['uniacid'],'id'=>$category['parentid']));
			if(!empty($parent['name'])){
				$categoryName = $parent['name']."-".$categoryName;
			}
		}
		return $categoryName;
	}
	public function getFullBrand(){
		global $_W;
		$sql = 'SELECT * FROM ' . tablename('imfox_eos_brand') . ' WHERE uniacid=:uniacid ';
		$allbrand = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));
		if (empty($allbrand)) {
			return array();
		}
		return $allbrand;
	}
	public function getBrandName($brand_id){
		global $_W;
		$brand = pdo_get("imfox_eos_brand",array('uniacid'=>$_W['uniacid'],'id'=>$brand_id));
		if(empty($brand['brandname'])){
			return "未知";
		}
		return $brand['brandname'];
	}
	public function getFullLevel(){
		global $_W;
		$sql = 'SELECT * FROM ' . tablename('imfox_eos_level') . ' WHERE uniacid=:uniacid ';
		$alllevel = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));
		if (empty($alllevel)) {
			return array();
		}
		return $alllevel;
	}
	public function getLevelName($level_id){
		global $_W;
		$level = pdo_get("imfox_eos_level",array('uniacid'=>$_W['uniacid'],'id'=>$level_id));
		if(empty($level['levelname'])){
			return "未选择等级";
		}
		$levelName = $level['levelname'];
		return $levelName;
	}
	public function getFullSupplier($wid){
		global $_W;
		$allSupplier = pdo_getall("imfox_eos_supplier", array('uniacid' => $_W['uniacid'],'wid'=>$wid));
		if (empty($allSupplier)) {
			return array();
		}
		return $allSupplier;
	}
	public function getSupplierName($supplier_id,$wid){
		global $_W;
		$supplier = pdo_get("imfox_eos_supplier",array('uniacid'=>$_W['uniacid'],'id'=>$supplier_id,'wid'=>$wid));
		if(empty($supplier['name'])){
			return "未设置";
		}
		$supplierName = $supplier['name'];
		return $supplierName;
	}
}
?>